from django.db import models


class Activity(models.Model):
    '''邮米活动'''

    PLATFORM_CHOICES = (
        (1, '广东'),
        (2, '广州')
    )

    ACTIVITY_STATE_CHOICES = (
        (1, '未开始'),
        (2, '进行中'),
        (3, '已暂停'),
        (4, '已结束')
    )

    activity_id = models.CharField(verbose_name='平台id', max_length=20, unique=True)
    name = models.CharField(verbose_name='活动名称', max_length=32, null=True, blank=True)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', null=True, blank=True)
    OrgId = models.CharField(verbose_name='所属机构', max_length=32, null=True, blank=True)
    platform = models.SmallIntegerField(verbose_name='平台', choices=PLATFORM_CHOICES, default=1, blank=True)
    available_begin_time = models.DateTimeField(verbose_name='开始时间', null=True, blank=True)
    available_end_time = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)
    max_point = models.DecimalField(verbose_name='总邮米', max_digits=10, decimal_places=0, default=0)
    distributed_point = models.DecimalField(verbose_name='派发邮米', max_digits=10, decimal_places=0, default=0)
    activity_state = models.SmallIntegerField(verbose_name='活动状态', choices=ACTIVITY_STATE_CHOICES, default=1, blank=True)
    create_user = models.ForeignKey('users.User', verbose_name='创建人', on_delete=models.SET_NULL, null=True, blank=True, related_name='activitycreate_set')
    owner = models.ForeignKey('users.User', verbose_name='所属人', on_delete=models.SET_NULL, null=True, blank=True, related_name='activityowner_set')
    take_point = models.DecimalField(verbose_name='占用邮米', max_digits=10, decimal_places=0, default=0)
    remain_point = models.DecimalField(verbose_name='剩余邮米', max_digits=10, decimal_places=0, default=0)
    use_point = models.DecimalField(verbose_name='已核销邮米', max_digits=10, decimal_places=0, default=0)
    activity_description=  models.TextField(verbose_name='活动使用说明', null=True, blank=True)

    class Meta:
        db_table = 'tb_activity'
        verbose_name = '邮米活动'
        verbose_name_plural = verbose_name


class ActivityData(models.Model):
    '''邮米领取明细'''

    STATE_CHOICES = (
        (1, '未领取'),
        (2, '已领取'),
        (3, '已使用'),
        (4, '已过期'),
        (5, '已撤销'),
        (6, '消耗完')
    )

    activity_data_id = models.CharField(verbose_name='平台id', max_length=32, unique=True)
    point = models.DecimalField(verbose_name='邮米', max_digits=10, decimal_places=0, default=0)
    create_time = models.DateTimeField(verbose_name='创建时间')
    received_time = models.DateTimeField(verbose_name='领取时间', null=True, blank=True)
    activity = models.ForeignKey('activities.Activity', verbose_name='活动', on_delete=models.CASCADE)
    customer_user = models.ForeignKey('users.CustomerUser', verbose_name='领取用户', on_delete=models.SET_NULL, null=True, blank=True)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    OrgId = models.CharField(verbose_name='所属机构', max_length=32, null=True, blank=True)
    state = models.SmallIntegerField(verbose_name='领取状态', choices=STATE_CHOICES, default=1, blank=True)
    companyuser = models.ForeignKey('users.User', verbose_name='派发客户经理', on_delete=models.SET_NULL, null=True, blank=True)
    old_openid = models.CharField(verbose_name='openid', max_length=32, null=True, blank=True)
    phone = models.CharField(verbose_name='客户经理手机号', max_length=11, null=True, blank=True)
    activity_level = models.ForeignKey('activities.ActivityLevel', verbose_name='活动分配记录', on_delete=models.SET_NULL, null=True, blank=True)
    common_url = models.CharField(verbose_name='链接', max_length=128, null=True, blank=True)
    common_qrcode = models.ImageField(verbose_name='普码', null=True, blank=True)
    identify = models.CharField(verbose_name='领取标识', max_length=32, null=True, blank=True)
    expired_time = models.DateTimeField(verbose_name='过期时间', null=True, blank=True)
    use_time = models.DateTimeField(verbose_name='使用时间', null=True, blank=True)
    remain_point = models.DecimalField(verbose_name='剩余邮米', max_digits=10, decimal_places=0, default=0)
    customeruservice = models.ForeignKey('users.CustomerUserVice', verbose_name='领取用户', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_activity_data'
        verbose_name = '邮米领取明细'
        verbose_name_plural = verbose_name


class ActivityLevel(models.Model):
    '''活动分配记录'''

    level = models.ForeignKey('users.Level', verbose_name='层级', on_delete=models.CASCADE)
    activity = models.ForeignKey('activities.Activity', verbose_name='活动', on_delete=models.CASCADE)
    max_point = models.DecimalField(verbose_name='总邮米', max_digits=10, decimal_places=0, default=0)
    take_point = models.DecimalField(verbose_name='占用邮米', max_digits=10, decimal_places=0, default=0)
    remain_point = models.DecimalField(verbose_name='剩余邮米', max_digits=10, decimal_places=0, default=0)
    use_point = models.DecimalField(verbose_name='已核销邮米', max_digits=10, decimal_places=0, default=0)
    receive_point = models.DecimalField(verbose_name='已领取邮米', max_digits=10, decimal_places=0, default=0)
    p_max_point = models.DecimalField(verbose_name='总邮米', max_digits=10, decimal_places=0, default=0)
    p_take_point = models.DecimalField(verbose_name='占用邮米', max_digits=10, decimal_places=0, default=0)
    p_remain_point = models.DecimalField(verbose_name='剩余邮米', max_digits=10, decimal_places=0, default=0)
    p_use_point = models.DecimalField(verbose_name='已核销邮米', max_digits=10, decimal_places=0, default=0)
    p_receive_point = models.DecimalField(verbose_name='已领取邮米', max_digits=10, decimal_places=0, default=0)

    class Meta:
        db_table = 'tb_activity_level'
        verbose_name = '活动分配记录'
        verbose_name_plural = verbose_name


class ActivityLevelRelation(models.Model):
    '''活动上下级'''

    parent = models.ForeignKey('activities.ActivityLevel', verbose_name='上级', on_delete=models.CASCADE, related_name='parent_set')
    child = models.ForeignKey('activities.ActivityLevel', verbose_name='下级', on_delete=models.CASCADE, related_name='child_set')

    class Meta:
        db_table = 'tb_activity_level_relation'
        verbose_name = '活动上下级'
        verbose_name_plural = verbose_name


class ActivityQRCode(models.Model):
    '''活动领取码'''

    QRCODE_STATE_CHOICES = (
        (1, '未使用'),
        (2, '已使用'),
        (3, '已过期')
    )

    companyuser = models.ForeignKey('users.User', verbose_name='派发客户经理', on_delete=models.SET_NULL, null=True, blank=True)
    common_url = models.CharField(verbose_name='链接', max_length=128, null=True, blank=True)
    common_qrcode = models.ImageField(verbose_name='普码', null=True, blank=True)
    identify = models.CharField(verbose_name='领取标识', max_length=32)
    activity_level = models.ForeignKey(ActivityLevel, verbose_name='所属地市活动', on_delete=models.SET_NULL, null=True, blank=True)
    qrcode_state = models.SmallIntegerField(verbose_name='二维码状态', choices=QRCODE_STATE_CHOICES, default=1, blank=True)
    gmt_distributed = models.DateTimeField(verbose_name='派发时间', null=True, blank=True)
    point = models.DecimalField(verbose_name='发放邮米', max_digits=10, decimal_places=0, default=0)

    class Meta:
        db_table = 'tb_activity_qrcode'
        verbose_name = '活动领取码'
        verbose_name_plural = verbose_name


class ActivityLevelRecord(models.Model):
    '''活动分配操作记录'''

    OPERATION_TYPE_CHOICES = (
        (1, '活动分配增加'),
        (2, '活动分配减少'),
        (3, '活动派发减少'),
        (4, '创建活动'),
        (5, '生产派发码减少'),
        (6, '派发码过期增加')
    )

    activity_level = models.ForeignKey('activities.ActivityLevel', verbose_name='活动分配记录', on_delete=models.CASCADE)
    point = models.DecimalField(verbose_name='邮米', max_digits=10, decimal_places=0, default=0)
    operation_type = models.SmallIntegerField(verbose_name='操作类型', choices=OPERATION_TYPE_CHOICES)
    remain_point = models.DecimalField(verbose_name='剩余邮米', max_digits=10, decimal_places=0, default=0)
    user = models.ForeignKey('users.User', verbose_name='操作人', on_delete=models.SET_NULL, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间')
    level = models.ForeignKey('users.Level', verbose_name='层级', on_delete=models.SET_NULL, null=True, blank=True)
    customer_user = models.ForeignKey('users.CustomerUser', verbose_name='领取用户', on_delete=models.SET_NULL, null=True, blank=True)
    remark = models.TextField(verbose_name='说明', null=True, blank=True)
    activity_data = models.ForeignKey('activities.ActivityData', verbose_name='派发二维码', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_activity_level_record'
        verbose_name = '活动分配记录'
        verbose_name_plural = verbose_name


class PointRecord(models.Model):
    '''用户邮米记录'''

    OPERATE_TYPE_CHOICES = (
        (1, '收入'),
        (2, '支出')
    )

    BUSINESS_TYPE_CHOICES = (
        (1, '邮米领取'),
        (2, '下单'),
        (3, '撤销邮米'),
        (4, '取消订单')
    )

    customer_user = models.ForeignKey('users.CustomerUser', verbose_name='领取用户', on_delete=models.SET_NULL, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='下单时间')
    operate_type = models.SmallIntegerField(verbose_name='收支类型', choices=OPERATE_TYPE_CHOICES)
    business_type = models.SmallIntegerField(verbose_name='业务类型', choices=BUSINESS_TYPE_CHOICES)
    point = models.DecimalField(verbose_name='邮米', max_digits=10, decimal_places=0, default=0)
    point_balance = models.DecimalField(verbose_name='邮米余额', max_digits=10, decimal_places=0, default=0)
    order = models.ForeignKey('orders.OrderInfo', verbose_name='关联订单', on_delete=models.SET_NULL, null=True, blank=True)
    activity_data = models.ForeignKey('activities.ActivityData', verbose_name='领取', on_delete=models.SET_NULL, null=True, blank=True)
    remain_point = models.DecimalField(verbose_name='剩余邮米', max_digits=10, decimal_places=0, default=0)

    class Meta:
        db_table = 'tb_point_record'
        verbose_name = '用户邮米记录'
        verbose_name_plural = verbose_name


class Channel(models.Model):
    '''渠道'''

    CHANNEL_TYPE_CHOICES = (
        (1, '微信'),
        (2, '支付宝')
    )
    id = models.BigIntegerField(verbose_name="ID", primary_key=True)
    cookie = models.TextField(verbose_name='登录cookie', null=True, blank=True)
    mchid = models.CharField(verbose_name='服务商号', max_length=20, null=True, blank=True)
    name = models.CharField(verbose_name='服务商名称', max_length=64, null=True, blank=True)
    serial_no = models.CharField(verbose_name='证书序列号', max_length=64, null=True, blank=True)
    api_key = models.CharField(verbose_name='api秘钥', max_length=64, null=True, blank=True)
    pub_key = models.TextField(verbose_name='V3平台证书', null=True, blank=True)
    ecc_csrf_token = models.CharField(verbose_name='', max_length=32, null=True, blank=True)
    channel_type = models.SmallIntegerField(verbose_name='平台', choices=CHANNEL_TYPE_CHOICES, default=1, blank=True)
    token = models.TextField(verbose_name='登录cookie', null=True, blank=True)

    class Meta:
        db_table = 'tb_channel'
        verbose_name = '渠道'
        verbose_name_plural = verbose_name


class LuckyBagActivity(models.Model):
    '''福袋活动'''

    ACTIVITY_STATUS_CHOICES = (
        (1, '未开始'),
        (2, '进行中'),
        (3, '已结束')
    )

    activity_name = models.CharField(verbose_name='活动名称', max_length=32)
    activity_status = models.SmallIntegerField(verbose_name='活动状态', choices=ACTIVITY_STATUS_CHOICES, default=1)
    available_begin_time = models.DateTimeField(verbose_name='开始时间', null=True, blank=True)
    available_end_time = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)
    description = models.TextField(verbose_name='活动说明', null=True, blank=True)
    explain = models.TextField(verbose_name='活动说明', null=True, blank=True)

    class Meta:
        db_table = 'tb_lucky_bag_activity'
        verbose_name = '福袋活动'
        verbose_name_plural = verbose_name


class LuckyBagChildActivity(models.Model):
    '''子活动'''

    ACTIVITY_TYPE_CHOICES = (
        (1, '日日消费礼'),
        (2, '周周榜单王'),
        (3, '支付宝大额交易抽奖'),
        (4, '国庆7天乐')
    )

    parent = models.ForeignKey('activities.LuckyBagActivity', verbose_name='父活动', on_delete=models.CASCADE)
    activity_type = models.SmallIntegerField(verbose_name='活动类型', choices=ACTIVITY_TYPE_CHOICES, default=1)
    description = models.TextField(verbose_name='活动说明', null=True, blank=True)
    limit_time_description = models.TextField(verbose_name='抽奖时间说明', null=True, blank=True)
    limit_day_description = models.TextField(verbose_name='抽奖时间说明', null=True, blank=True)
    banner = models.ImageField(verbose_name='活动封面', null=True, blank=True)
    available_end_time = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)
    available_begin_time = models.DateTimeField(verbose_name='开始时间', null=True, blank=True)
    sort = models.IntegerField(verbose_name='排序', null=True, blank=True)

    class Meta:
        db_table = 'tb_lucky_bag_child_activity'
        verbose_name = '子活动'
        verbose_name_plural = verbose_name


class LuckyBagChildActivityDistributedLimit(models.Model):
    '''子活动时间限制'''

    LIMIT_BY_TIME_CHOICES= (
        (1, '按周计'),
        (2, '固定时间')
    )

    luckybagchildactivity = models.ForeignKey('activities.LuckyBagChildActivity', verbose_name='子活动', on_delete=models.CASCADE)
    weeks = models.CharField(verbose_name='每周', max_length=128)
    start_enable_time = models.DateTimeField(verbose_name='生效时间')
    end_enable_time = models.DateTimeField(verbose_name='失效时间')
    limit_by_time = models.SmallIntegerField(verbose_name='参与限时', choices=LIMIT_BY_TIME_CHOICES, default=1, blank=True)

    class Meta:
        db_table = "tb_lucky_bag_child_activity_distributed_limit"
        verbose_name = '子活动时间限制'
        verbose_name_plural = verbose_name


class LuckyBagChildActivityRule(models.Model):
    '''抽奖规则'''

    IS_OCCUPY_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    ACTIVITY_TYPE_CHOICES = (
        (1, '日日消费礼'),
        (4, '国庆7天乐')
    )

    lucky_bag = models.DecimalField(max_digits=10, decimal_places=0, verbose_name='福袋', default=0)
    validity_time = models.DateTimeField(verbose_name='奖品有效期')
    is_occupy = models.SmallIntegerField(verbose_name='是否占用', choices=IS_OCCUPY_CHOICES, default=0)
    business_no = models.CharField(verbose_name='商户号', max_length=10, null=True, blank=True)
    activity_type = models.SmallIntegerField(verbose_name='活动类型', choices=ACTIVITY_TYPE_CHOICES, default=1)

    class Meta:
        db_table = "tb_lucky_bag_child_activity_rule"
        verbose_name = '抽奖规则'
        verbose_name_plural = verbose_name


class LuckyBagMerchantOrder(models.Model):
    '''商户达标订单'''

    IS_STANDARD_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    ACTIVITY_TYPE_CHOICES = (
        (1, '日日消费礼'),
        (4, '国庆7天乐')
    )

    trade_time = models.DateTimeField(verbose_name='交易日期')
    business_no = models.CharField(verbose_name='商户号', max_length=10)
    is_standard = models.SmallIntegerField(verbose_name='是否达标', choices=IS_STANDARD_CHOICES, default=0)
    trade_no = models.CharField(verbose_name='订单号', max_length=32, null=True, blank=True)
    trade_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='交易金额', default=0)
    activity_type = models.SmallIntegerField(verbose_name='活动类型', choices=ACTIVITY_TYPE_CHOICES, default=1)

    class Meta:
        db_table = "tb_lucky_bag_merchant_order"
        verbose_name = '商户达标订单'
        verbose_name_plural = verbose_name


class LuckyBagChildActivityData(models.Model):
    '''福袋领取明细'''

    LUCKY_STATUS_CHOICES = (
        (0, '待抽奖'),
        (1, '待领取'),
        (2, '已领取'),
        (3, '已兑换'),
        (4, '已过期')
    )

    merchantuser = models.ForeignKey('merchants.MerchantUser', verbose_name='所属用户', on_delete=models.SET_NULL, null=True, blank=True)
    business_no = models.CharField(verbose_name='商户号', max_length=10)
    lucky_bag = models.DecimalField(max_digits=10, decimal_places=0, verbose_name='福袋', default=0)
    lucky_status = models.SmallIntegerField(verbose_name='福袋状态', choices=LUCKY_STATUS_CHOICES, default=0)
    expired_time = models.DateTimeField(verbose_name='过期时间', null=True, blank=True)
    open_time = models.DateTimeField(verbose_name='抽奖时间', null=True, blank=True)
    received_time = models.DateTimeField(verbose_name='抽奖时间', null=True, blank=True)
    index = models.IntegerField(verbose_name='抽奖顺位', null=True, blank=True)
    luckybagchildactivity = models.ForeignKey('activities.LuckyBagChildActivity', verbose_name='子活动', on_delete=models.CASCADE)
    level = models.ForeignKey('users.Level', verbose_name='层级', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_lucky_bag_child_activity_data'
        verbose_name = '福袋领取明细'
        verbose_name_plural = verbose_name


class LuckyBagRecord(models.Model):
    '''用户福袋记录'''

    OPERATE_TYPE_CHOICES = (
        (1, '抽奖'),
        (2, '提现')
    )

    merchantuser = models.ForeignKey('merchants.MerchantUser', verbose_name='所属用户', on_delete=models.SET_NULL, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='下单时间')
    operate_type = models.SmallIntegerField(verbose_name='收支类型', choices=OPERATE_TYPE_CHOICES)
    cashactivitydata = models.OneToOneField('cashbonus.CashActivityData', verbose_name='兑换金额', on_delete=models.SET_NULL, null=True, blank=True)
    activitydata = models.ForeignKey('activities.LuckyBagChildActivityData', verbose_name='领取', on_delete=models.SET_NULL, null=True, blank=True)
    lucky_bag = models.DecimalField(max_digits=10, decimal_places=0, verbose_name='福袋', default=0)
    lucky_bag_balance = models.DecimalField(max_digits=10, decimal_places=0, verbose_name='福袋余额', default=0)

    class Meta:
        db_table = 'tb_lucky_bag_record'
        verbose_name = '用户福袋记录'
        verbose_name_plural = verbose_name
